package com.atguigu.gulimall.product.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author LiQianJin
 * @email 18410584912@qq.com
 * @date 2024-03-30 14:43:23
 */
@RestController
@RequestMapping("product/attr")
@Slf4j
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R queryBaseAttrListPage(@RequestParam Map<String, Object> params,
                                   @PathVariable("attrType") String attrType,
                                   @PathVariable("catelogId") Long catelogId) {
        try {
            log.info("AttrController.queryBaseAttrListPage.params:{},attrType:{},catelogId:{}", JSON.toJSONString(params), attrType, catelogId);
            PageUtils page = attrService.queryBaseAttrListPage(params, attrType, catelogId);
            return R.ok().put("page", page);
        } catch (Exception e) {
            log.error("AttrController.queryBaseAttrListPage.error:{}", e, e.getMessage());
            return R.error(e.getMessage());
        }

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrEntity attr) {
        attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr) {
        attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
